#include "LinkSerialClient.h"
#include "PacketModbus.h"
#include <iostream>
#include "lib.h"
#include "IMode.h"

using namespace controller;
using namespace std;


void test_fn(std::function<string ()> const &fn)
{
    cout << "打印函数返回值" << fn() << endl;
}




void test_process_modbus(void)
{
    PacketModbus packet_modbus;
    LinkSerialPort l;
    map<string, string> arg = {
        {"dev", "/dev/ttyv0"},
        //{"dev","/dev/ttyS0"},
        {"baud", "115200"}};
    l.open(arg);
    l.set_dump_buffer_type(true);
    packet_modbus.build(0x55, 0x06, 0x1234, 0x02);
    cout << packet_modbus.to_string();

    //lambda
    test_fn([&](){ return "kaihua";});

    // IMode::S2(&l, (PacketBase *)&packet_modbus, (PacketBase *)&packet_modbus, [&](LinkBase *l, PacketBase *r)
    //           { cout << "我用对象发送，我真的接收到了一个数据包啦" << endl; }, 10000);

    // 使用函数发送数据包
    IMode::S2(&l, [&](LinkBase *l) {
            //通过函数返回数据包
            cout << "函数生成一个包"<<endl;
            PacketModbus*pm = new PacketModbus();
            pm->build(0x11,0x01,0xFF11,3);
            return pm;
    },(PacketBase *)&packet_modbus, 
    [&](LinkBase *l, PacketBase *r){ 
        cout << "我用对象发送，我真的接收到了一个数据包啦" << endl; 
    }, 10000);

    IMode::W(&l, &packet_modbus, [&](LinkBase *l, PacketBase *p)
             {
        static int c = 0;
        cout << "第 " << to_string(c) << " 数据包"  << endl;
        c++; }, 10000, 5);

    l.close();
}